//
//  SEAuthSwitchViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/12.
//

import UIKit

class SEAuthContainerViewController: SEBaseViewController {

    var loginView: UIView?
    var registerView: UIView?

    var isSelected = false

    override func viewDidLoad() {
        super.viewDidLoad()
        let bgView = UIView()
        view.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.margins.equalToSuperview()
        }
        let loginVc = SELoginViewController()
        loginView = loginVc.view
        loginView?.frame = bgView.bounds
        let registerVc = SERegisterViewController()
        registerView = registerVc.view
        registerView?.frame = bgView.bounds
        bgView.addSubview(registerView!)
        bgView.addSubview(loginView!)
        // Do any additional setup after loading the view.
    }

    override func setupNavBar() {
        super.setupNavBar()
        navigationItem.leftBarButtonItem = UIBarButtonItem(
            image: Asset.navCloseIcon.image.withRenderingMode(.alwaysOriginal),
            style: .plain,
            target: self, action: #selector(leftItemAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(switchViews))
    }

    @objc private func leftItemAction() {
        dismiss(animated: true, completion: nil)
    }

    @objc func switchViews() {
        if !isSelected {
            UIView.transition(from: loginView!, to: registerView!, duration: 0.5, options: [.transitionFlipFromLeft]) { (finish) in
                self.isSelected = true
                self.navigationItem.rightBarButtonItem?.title = "登录"
            }
        } else {
            UIView.transition(from: registerView!, to: loginView!, duration: 0.5, options: [.transitionFlipFromRight]) { (finish) in
                self.isSelected = false
                self.navigationItem.rightBarButtonItem?.title = "注册"
            }
        }
    }

}
